package com.hmy.finance.controller.statement;


import com.hmy.ccp.common.response.BasePageResponse;
import com.hmy.ccp.common.result.JsonResult;
import com.hmy.ccp.error.annotations.HmyGateWayController;
import com.hmy.ccp.error.exception.HMYRuntimeException;
import com.hmy.finance.bo.request.*;
import com.hmy.finance.bo.response.*;
import com.hmy.finance.service.core.IRevenueService;
import com.hmy.finance.service.statement.IReconciliationStatementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.List;

@Api(tags = "账单管理")
@RestController
@RequestMapping("/api/statement")
@Validated
@Slf4j
@HmyGateWayController
public class StatementController {

    @Resource
    private IReconciliationStatementService iReconciliationStatementService;

    @Resource
    private IRevenueService revenueService;

    /**
     * 分页查询历史账单
     */
    @PostMapping("/queryStatementsByPage")
    public JsonResult<BasePageResponse<HistoryStatementQueryResponse>> queryStatementsByPage(@RequestBody @Validated HistoryStatementQueryRequest historyStatementQueryRequest) {
        BasePageResponse<HistoryStatementQueryResponse> response = iReconciliationStatementService.queryStatementsByPage(historyStatementQueryRequest);
        return JsonResult.success(response);
    }


    /**
     * 查询合同及设备信息
     */
    @PostMapping("/queryContractDevicesInfo")
    public JsonResult<List<ContractDeviceInfoResponse>> queryContractDevicesInfo(@RequestBody @Validated ContractDevicesInfoQueryRequest contractDevicesInfoQueryRequest) {
        List<ContractDeviceInfoResponse> response = iReconciliationStatementService.queryContractDevicesInfo(contractDevicesInfoQueryRequest);
        return JsonResult.success(response);
    }

    /**
     * 创建合同账单
     */
    @PostMapping("/createStatement")
    public JsonResult<CreateStatementResponse> createStatement(@RequestBody @Validated CreateStatementRequest createStatementRequest) {
        List<CreateStatementDeviceRequest> createStatementDevices = createStatementRequest.getDeviceDetails();
        LocalDate today = LocalDate.now();
        createStatementDevices.forEach(createStatementDevice -> {
            LocalDate setPreStartTime = createStatementDevice.getSetPreStartTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            if (today.isEqual(setPreStartTime)) {
                throw new HMYRuntimeException(506300016);
            }
        });
        CreateStatementResponse response = iReconciliationStatementService.createStatement(createStatementRequest);
        return JsonResult.success(response);
    }

    /**
     * 设备账单时间计算
     */
    @PostMapping("/calculateStatementDeviceStartTime")
    public JsonResult<CalculateStatementResponse> calculateStatementDeviceStartTime(@RequestBody @Validated CalculateStatementRequest calculateStatementRequest) {
        CalculateStatementResponse response = iReconciliationStatementService.calculateStatementDeviceStartTime(calculateStatementRequest);
        return JsonResult.success(response);
    }


    /**
     * 账单页面在途单据信息展示
     * 获取各类在途单据信息展示
     */
    @PostMapping("/getInTransitDocumentInfos")
    public JsonResult<InTransitDocumentInfoResponse> getInTransitDocumentInfos(@RequestBody @Validated InTransitDocumentInfoRequest inTransitDocumentInfoRequest) {
        InTransitDocumentInfoResponse response = iReconciliationStatementService.getInTransitDocumentInfos(inTransitDocumentInfoRequest);
        return JsonResult.success(response);
    }

    /**
     * 账单删除接口
     *
     * @param deleteStatementRequest deleteStatementRequest
     */
    @PostMapping("/deleteStatement")
    public JsonResult<DeleteStatementResponse> deleteStatement(@RequestBody @Validated DeleteStatementRequest deleteStatementRequest) {
        DeleteStatementResponse response = iReconciliationStatementService.deleteStatement(deleteStatementRequest);
        return JsonResult.success(response);
    }

    /**
     * 校验是否能够创建账单
     *
     * @param createStatementValidationRequest createStatementValidationRequest
     */
    @PostMapping("/validationCreateStatement")
    public JsonResult<CreateStatementValidationResponse> validationCreateStatement(@RequestBody @Validated CreateStatementValidationRequest createStatementValidationRequest) {
        CreateStatementValidationResponse response = iReconciliationStatementService.validationCreateStatement(createStatementValidationRequest);
        return JsonResult.success(response);
    }


    /**
     * 对账管理-总营收相关汇总数据查询
     *
     * @param request
     * @return
     */
    @PostMapping("/queryReceivablesSum")
    public JsonResult<ReceivablesSumQueryResponse> queryReceivablesSum(@RequestBody @Validated ReceivablesSumQueryRequest request) {
        ReceivablesSumQueryResponse response = revenueService.queryReceivablesSum(request);
        return JsonResult.success(response);
    }

    /**
     * 对账管理-设备拉取相关汇总数据查询
     *
     * @param request
     * @return
     */
    @PostMapping("/queryDeviceReceivablesSum")
    public JsonResult<ReceivablesDeviceSumQueryResponse> queryDeviceReceivablesSum(@RequestBody @Validated ReceivablesDeviceQueryRequest request) {
        ReceivablesDeviceSumQueryResponse response = revenueService.queryDeviceReceivablesSum(request);
        return JsonResult.success(response);
    }

    /**
     * 对账管理-设备拉取分页列表
     *
     * @param request
     * @return
     */
    @PostMapping("/pageQueryReceivablesDevice")
    public JsonResult<BasePageResponse<ReceivablesDevicePageQueryResponse>> pageQueryReceivablesDevice(@RequestBody @Validated ReceivablesDeviceQueryRequest request) {
        BasePageResponse<ReceivablesDevicePageQueryResponse> response = revenueService.pageQueryReceivablesDevice(request);
        List<ReceivablesDeviceQueryRequest.ContractDeviceDetailRequest> createStatementDevices = request.getDeviceDetails();
        LocalDate today = LocalDate.now();
        createStatementDevices.forEach(createStatementDevice -> {
            LocalDate setPreStartTime = createStatementDevice.getSetPreStartTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            if (today.isEqual(setPreStartTime)) {
                throw new HMYRuntimeException(506300016);
            }
        });

        return JsonResult.success(response);
    }
}
